import Koa from "koa";
import router from "./router";
import { Server } from "http";
const mongoose = require("mongoose");

const app = new Koa();

const MONGO_URL = "mongodb://cms1:123456@47.122.68.86:27017/cms1";

mongodb: mongoose
  .connect(MONGO_URL)
  .then(() => console.log("MongoDB connected"))
  .catch((err: Error) => console.error(err));

app.use(async (ctx, next) => {
  console.log("Request URL:", ctx.url);
  await next();
});
app.use(router.routes());
app.use(router.allowedMethods());

router.prefix("/napi");
const runServer = (port: number): Server => {
  console.log(`Server running on port ${port}`);
  const server = app.listen(port);
  return server;
};

export default runServer;
